package com.bw.controller;

import com.bw.entity.Dict;
import com.bw.entity.PeopleInfo;
import com.bw.entity.PeopleQuery;
import com.bw.mapper.PeopleInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ListController {


    @Autowired
    PeopleInfoMapper peopleInfoMapper;

    /**
     * 列表页 以及点击查询
     *
     * @return
     */
    @RequestMapping("/list.do")
    public String list(PeopleQuery query, Integer pageNum, Model model) {
        //1. 渲染下拉框
        List<Dict> postList = peopleInfoMapper.listPost();
        model.addAttribute("postList", postList);

        List<Dict> sourceList = peopleInfoMapper.listSource();
        model.addAttribute("sourceList", sourceList);

        List<Dict> resultList = peopleInfoMapper.listResult();
        model.addAttribute("resultList", resultList);
        // 2. 分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 3);
        List<PeopleInfo> list = peopleInfoMapper.selectList(query);

        PageInfo<PeopleInfo> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);

        //3. 查询条件回显
        model.addAttribute("query", query);
        return "list";
    }

    /**
     * 返回一个添加页面
     * @return
     */
    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        //1. 渲染下拉框
        List<Dict> postList = peopleInfoMapper.listPost();
        model.addAttribute("postList", postList);

        List<Dict> sourceList = peopleInfoMapper.listSource();
        model.addAttribute("sourceList", sourceList);

        List<Dict> resultList = peopleInfoMapper.listResult();
        model.addAttribute("resultList", resultList);
        return "add";
    }

    /**
     * 执行添加
     * @return
     */
    @RequestMapping("/add.do")
    public String add(){


        //重定向：  告诉浏览器 接下来去访问 list.do
        return "redirect:list.do";  // 这个不再返回一个页面了
//        return "list";
    }
}
